﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;
using System.Data;

namespace Visa.Service
{
    public class CauhoiService
    {
        private VisaEntities db = new VisaEntities();
        private StringHelpersService StringHl = new StringHelpersService();

        public int addOrUpdateNews(Cauhoi entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void Delete(int id)
        {
            var c = db.Cauhois.SingleOrDefault(l => l.Id == id);
            c.Detele = true;
            db.SaveChanges();
        }

        public Cauhoi GetById(int id)
        {
            try
            {
                return db.Cauhois.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public List<Cauhoi> GetAll()
        {
            try
            {
                return db.Cauhois.Where( m => m.Detele == false).OrderByDescending( l => l.DateCreate).ToList();
            }
            catch { return null; }
        }

        public List<Cauhoi> GetCauhoiByQuocGiaOrRegon(string reqcode)
        {
            try
            {
                return db.Cauhois.Where(m => m.Detele == false && m.regioncode == reqcode).OrderByDescending(l => l.DateCreate).ToList();
            }
            catch { return null; }
        }

        public SearchTuVanEntity GetAllBYQuocGia(string txt)
        {
            try
            {
                SearchTuVanEntity listFinnal = new SearchTuVanEntity();
                listFinnal.cauhoientity = new List<Cauhoi>();
                listFinnal.thongtinhuuichentity = new List<Thongtinhuuich>();

                txt = StringHl.RemoveSign4VietnameseString(txt).ToLower();
                string[] strSearch = txt.Split(' ');

                var c = db.Cauhois.ToList();
                var d = db.Thongtinhuuiches.ToList();

                var listcauhoi = c.FindAll(
                                delegate(Cauhoi math)
                                {
                                    if (strSearch.Any(k => StringHl.RemoveSign4VietnameseString(math.title.ToLower()).Contains(k)))
                                        return true;
                                    else
                                        return false;
                                } );

                var listthongtin = d.FindAll(
                                delegate(Thongtinhuuich math)
                                {
                                    if (strSearch.Any(k => StringHl.RemoveSign4VietnameseString(math.Name.ToLower()).Contains(k)))
                                        return true;
                                    else
                                        return false;
                                });

                listFinnal.cauhoientity.AddRange(listcauhoi);
                listFinnal.thongtinhuuichentity.AddRange(listthongtin);

                return listFinnal;
            }
            catch { return null; }
        }


        public int addOrUpdateTraloi(Traloi entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void DeleteTraloi(int id)
        {
            var c = db.Tralois.SingleOrDefault(l => l.Id == id);
            db.Tralois.Remove(c);
            db.SaveChanges();
        }

        public void UpdateTraloi(int id)
        {
            var c = db.Cauhois.SingleOrDefault(l => l.Id == id);
            c.Answered = true;
            db.SaveChanges();
        }

        public Traloi GetByTraloiId(int id)
        {
            try
            {
                return db.Tralois.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public Traloi GetTraloiByCauhoiId(int id)
        {
            try
            {
                return db.Tralois.Where(l => l.CauhoiId == id).OrderByDescending( o => o.DateCreate).First();
            }
            catch { return null; }
        }
    }
}
